<template>
  <el-dialog v-if="popenevTVisible" v-dialogDrag title="发布" :visible.sync="popenevTVisible" width="40%" :before-close="handleCancel" :close-on-click-modal="false">
    <!-- 选择人员 -->
    <el-collapse v-model="activeNames">
      <el-collapse-item title="选择人员" name="1" v-if="showFbObj.person">
        <template slot="title">
          <el-link type="primary">选择人员</el-link>
        </template>
        <el-input v-model="personNames" rows="5" type="textarea" @focus="choicePerson" />
      </el-collapse-item>
      <el-collapse-item title="选择群组" name="2" v-if="showFbObj.group">
        <template slot="title">
          <el-link type="primary">选择群组</el-link>
        </template>
        <el-input v-model="groupNames" rows="5" type="textarea" @focus="choiceGroup" />
      </el-collapse-item>
      <el-collapse-item title="选择群组" name="3" v-if="showFbObj.orgGroup">
        <template slot="title">
          <el-link type="primary">选择机构群组</el-link>
        </template>
        <el-input v-model="groupOrgNames" rows="5" type="textarea" @focus="choiceOrgGroup" />
      </el-collapse-item>
      <el-collapse-item title="选择单位" name="4" v-if="showFbObj.org">
        <template slot="title">
          <el-link type="primary">选择单位</el-link>
        </template>
        <el-input v-model="orgNames" rows="5" type="textarea" @focus="choiceOrg" />
      </el-collapse-item>
      <el-collapse-item title="选择文书" name="5" v-if="showFbObj.ws">
        <template slot="title">
          <el-link type="primary">选择文书</el-link>
        </template>
        <el-input v-model="wsOrgNames" rows="5" type="textarea" @focus="choiceWsOrg" />
      </el-collapse-item>
    </el-collapse>

    <!-- </el-scrollbar> -->
    <!-- 搜索框 -->
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="loading" @click="handleOk">发布</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getPersonListByCompanyIdAndRoleName } from "../../api/system/person/api";
import { getCompanyTreeAndSubCompany } from "../../api/system/organzation/api";
import { getpublishDoc } from "../../api/common/flowsend";
import showChoiceOrg from '../choiceOrg/index';
import showChoicePerson from "../choicePerson/index";
import showChoiceGroup from "../choceGroup/index";
import showChoiceOrgGroup from '../choiceOrgGroup/index';
export default {
  data()
  {
    return {
      //流程节点unid
      flowstructureunid: null,
      formUnid: null,
      loading: false,
      moduleName: null,
      docIdent: null,
      // 显示选择的人员
      personNames: "",
      //显示选择的机构
      orgNames: "",
      // 选择人员数据
      personData: [],
      //选择机构数据
      orgData: [],
      //显示的群组名称
      groupNames: "",
      //显示的机构群组名称
      groupOrgNames: "",
      //选择的群组数据
      groupData: [],
      //选择的机构群组数据
      groupOrgData: [],
      //本单位和二级单位
      orgTree: [],
      //文书机构列表
      wsOrgData: [],
      //文书机构名称
      wsOrgNames: "",
      //文书人员信息
      wsPersonDate: [],
      activeNames: ["1", "2", "3", "4", "5"],
      popenevTVisible: true,
      //promise 状态
      promiseStatus: null,
      showFbObj: window.HC_config.VUE_APP_SHOW_FB || {
        person: true,
        group: true,
        org: window.HC_config.VUE_APP_SHOW_FB_ORG != undefined && window.HC_config.VUE_APP_SHOW_FB_ORG != null ? window.HC_config.VUE_APP_SHOW_FB_ORG : true,
        orgGroup: window.HC_config.VUE_APP_SHOW_ORGGROUP || false,
        ws: window.HC_config.VUE_APP_SHOW_FB_WSORG || false,
      }
    };
  },
  methods: {
    show(showData)
    {
      this.moduleName = showData.moduleName;
      this.formUnid = showData.unid;
      if ((showData.zsdw && showData.zsdwId) || (showData.csdw && showData.csdwId))
      {
        var zsdw = showData.zsdw != '' ? showData.zsdw.split(',') : []
        var csdw = showData.csdw != '' ? showData.csdw.split(',') : []
        var zsdwId = showData.zsdwId != '' ? showData.zsdwId.split(',') : []
        var csdwId = showData.csdwId != '' ? showData.csdwId.split(',') : []
        var orgName = [...zsdw, ...csdw]
        var orgUnid = [...zsdwId, ...csdwId]

        //通过主送抄送获取发布单位
        if (orgName.length == orgUnid.length && window.HC_config.VUE_APP_USEZSCSFB)
        {
          let tempObj = {};
          let orgNameArr = [];
          orgName.map((pre_orgName, index) =>
          {
            tempObj[orgUnid[index]] = pre_orgName;
          })
          for (let key in tempObj)
          {
            this.orgData.push({
              unid: key,
              orgCommonname: tempObj[key]
            })
            orgNameArr.push(tempObj[key]);
          }
          this.orgNames = orgNameArr.join(",");
        }
        //通过主送抄送获取文书
        if(window.HC_config.VUE_APP_WITH_ZSDW)
        {
          var orgData = [];
          orgName.map((org, index) =>
          {
            var obj = {}
            obj.orgCommonname = org
            obj.unid = orgUnid[index]
            orgData.push(obj)
          })
          this.getWsData(orgData)
        }
      }
      if (this.showWsOrg)
      {
        //本单位和二级单位
        getCompanyTreeAndSubCompany().then(res =>
        {
          this.orgTree = res.result || [];
        })
      }

      // 创建promise
      return new Promise((resolve, reject) =>
      {
        this.promiseStatus = {
          resolve,
          reject,
        };
      });
    },
    // 获取文书
    getWsData(P_Data)
    {
      let personName = [];
      let dwUnids = [];
      P_Data.forEach(element =>
      {
        dwUnids.push(element.unid);
      });
      this.loading = true;
      getPersonListByCompanyIdAndRoleName({
        orgUnids: dwUnids.join(","),
        roleNames: "收文总文书"
      }).then(res =>
      {
        this.loading = false;
        this.personData = res.result || []
        this.personData.forEach(pre_person =>
        {
          personName.push(pre_person.personCommonname);
          this.personNames = personName.join("、")
        })
      }).finally(() =>
      {
        this.loading = false;
      })
    },
    handleOk()
    {
      this.loading = true;
      //人员
      let personUnids = [];
      this.personData.forEach(item =>
      {
        personUnids.push(item.unid);
      })

      //文书人员
      this.wsPersonDate.forEach(item =>
      {
        personUnids.push(item.unid);
      })

      let orgUnids = [];
      this.orgData.forEach(item =>
      {
        orgUnids.push(item.unid);
      })


      let groupUnids = [];
      this.groupData.forEach(item =>
      {
        groupUnids.push(item.unid);
      })

      let params = {
        flowstructureunid: this.flowstructureunid,
        moduleName: this.moduleName,
        docIdent: this.formUnid,
        sendFlowTrack: true,
        sendBacklog: true,
        docUrl: this.$route.path,
        personUnids: personUnids.join(','),
        orgUnids: orgUnids.join(','),
        groupUnids: groupUnids.join(',')
      }
      getpublishDoc(params)
        .then((res) =>
        {
          this.$message.success("发布成功");
          // this.$message.success("发布成功");
          this.promiseStatus.resolve(res);
          this.destroyElement();
        }).catch(err =>
        {
        }).finally(() =>
        {
          this.loading = false;
        });
    },
    handleCancel()
    {
      //清空勾选项
      this.promiseStatus.reject({
        msg: "取消",
      });
      this.destroyElement();
    },
    /**
     * cs 2022-4-8
     * 销毁组件
     */
    destroyElement()
    {
      this.popenevTVisible = false;
    },
    /**
     * 选择人员
     */
    choicePerson()
    {
      showChoicePerson({ showData: this.personData, multiple: true }).then(res =>
      {

        this.personData = res.choiceData || [];
        let tampArr = [];
        this.personData.forEach(element =>
        {
          tampArr.push(element.personCommonname)
        });
        this.personNames = tampArr.join(",")
      })
    },
    /**
     * 选择机构
     */
    choiceOrg()
    {
      showChoiceOrg({ showData: this.orgData, multiple: true }).then(res =>
      {
        this.orgData = res.choiceData || [];
        let tampArr = [];
        this.orgData.forEach(element =>
        {
          tampArr.push(element.orgCommonname)
        });
        this.orgNames = tampArr.join(",")
      })
    },
    choiceWsOrg()
    {
      showChoiceOrg({ showData: this.wsOrgData, multiple: true }, {
        checkStrictly: true,
        treeList: this.orgTree
      }).then(res =>
      {
        this.wsOrgData = res.choiceData || [];
        let tampArr = [];
        let personName = [];
        let dwUnids = [];
        this.wsOrgData.forEach(element =>
        {
          dwUnids.push(element.unid);
          tampArr.push(element.orgCommonname)
        });
        this.loading = true;
        getPersonListByCompanyIdAndRoleName({
          orgUnids: dwUnids.join(","),
          roleNames: "收文总文书"
        }).then(res =>
        {
          this.wsPersonDate = res.result || [];
          this.wsPersonDate.forEach(pre_person =>
          {
            this.loading = false;
            personName.push(pre_person.personCommonname);
            this.wsOrgNames = tampArr.join(",") + "(" + personName.join("、") + ")";
          })
        })
      })
    },
    /**
     * 选择群组
     */
    choiceGroup()
    {
      showChoiceGroup({ showData: this.groupData, multiple: true }).then(res =>
      {
        console.log(res)
        this.groupData = res.choiceData || [];
        // let tampArr = [];
        // this.groupData.forEach(element => {
        //   tampArr.push(element.name)
        // });
        this.groupNames = res.personNameStr;
        // this.groupNames = tampArr.join(",")
      })
    },
    /**
     * 选择机构群组
     */
    choiceOrgGroup()
    {
      showChoiceOrgGroup({ showData: this.groupOrgData, multiple: true }).then(res =>
      {
        console.log(res, '回调')
        this.groupOrgData = res.choiceData || []
        this.groupOrgNames = res.personNameStr;
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.scorll-Y {
  height: 400px;
  margin-top: 10px;
  ::v-deep .el-scrollbar__wrap {
    overflow-x: hidden;
  }

  .card-box {
    height: 200px;
    margin: 10px 0;
    .box-head {
      height: 37px;
      // line-height: 37px;
      background-color: #eeeeee;
      padding: 0 5px;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }
}
</style>